package com.javaspirit.android.diary.activity;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.javaspirit.android.diary.R;
import com.javaspirit.android.diary.dal.DiaryContentProvider;
import com.javaspirit.android.diary.dal.NoteTable;

public class ListNoteFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

	// This is the Adapter being used to display the list's data.
	SimpleCursorAdapter mAdapter;

	// If non-null, this is the current filter the user has provided.
	String mCurFilter;
	
	// These are the Contacts rows that we will retrieve.
	static final String[] NOTE_SUMMARY_PROJECTION = new String[] { 
		NoteTable.COLUMN_ID,
		NoteTable.COLUMN_DATE, 
		NoteTable.COLUMN_MOOD, 
		NoteTable.COLUMN_WEATHER, 
		NoteTable.COLUMN_LAST_MOD_TS, 
		NoteTable.COLUMN_NOTE };
	
	static final int[] NOTE_SUMMARY_LAYOUT = new int[] { 
		R.id.nli_tv_id,
		R.id.nli_tv_date, 
		R.id.nli_tv_mood, 
		R.id.nli_tv_weather, 
		R.id.nli_tv_timestamp, 
		R.id.nli_tv_note };
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		
		super.onActivityCreated(savedInstanceState);

		// Create an empty adapter we will use to display the loaded data.
		mAdapter = new SimpleCursorAdapter(getActivity(),
				R.layout.notelistitem, null,
				NOTE_SUMMARY_PROJECTION,
				NOTE_SUMMARY_LAYOUT, 0);
		
		setListAdapter(mAdapter);

		// Start out with a progress indicator.
		setListShown(false);

		// Prepare the loader. Either re-connect with an existing one,
		// or start a new one.
		getLoaderManager().initLoader(0, null, this);
	}

	//
	// @Override public void onCreateOptionsMenu(Menu menu, MenuInflater
	// inflater) {
	// // Place an action bar item for searching.
	// MenuItem item = menu.add("Search");
	// item.setIcon(android.R.drawable.ic_menu_search);
	// MenuItemCompat.setShowAsAction(item,
	// MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
	// View searchView = SearchViewCompat.newSearchView(getActivity());
	// if (searchView != null) {
	// SearchViewCompat.setOnQueryTextListener(searchView,
	// new OnQueryTextListenerCompat() {
	// @Override
	// public boolean onQueryTextChange(String newText) {
	// // Called when the action bar search text has changed. Update
	// // the search filter, and restart the loader to do a new query
	// // with this filter.
	// mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
	// getLoaderManager().restartLoader(0, null, CursorLoaderListFragment.this);
	// return true;
	// }
	// });
	// MenuItemCompat.setActionView(item, searchView);
	// }
	// }

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// Insert desired behavior here.
		Log.i("FragmentComplexList", "Item clicked: " + id);
		Intent viewNoteIntent = new Intent(getActivity(), ViewNoteActivity.class);
		viewNoteIntent.putExtra(NoteTable.COLUMN_ID, id);
		startActivity(viewNoteIntent);
	}


	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		// This is called when a new Loader needs to be created. This
		// sample only has one Loader, so we don't care about the ID.
		// First, pick the base URI to use depending on whether we are
		// currently filtering.
		
		 return new CursorLoader(getActivity(), DiaryContentProvider.CONTENT_URI_NOTE,
				 NOTE_SUMMARY_PROJECTION, null, null, NoteTable.COLUMN_ID+" DESC");
		 
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		// Swap the new cursor in. (The framework will take care of closing the
		// old cursor once we return.)
		mAdapter.swapCursor(data);

		// The list should now be shown.
		if (isResumed()) {
			setListShown(true);
		} else {
			setListShownNoAnimation(true);
		}
	}

	public void onLoaderReset(Loader<Cursor> loader) {
		// This is called when the last Cursor provided to onLoadFinished()
		// above is about to be closed. We need to make sure we are no
		// longer using it.
		mAdapter.swapCursor(null);
	}
}
